package com.zzmcc.jms.controller;

import com.jfinal.aop.Before;
import com.jfinal.ext.render.excel.JxlsRender;
import com.jfinal.kit.PathKit;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.zzmcc.base.BaseController;
import com.zzmcc.common.AuthorizeInterceptor;
import com.zzmcc.jms.model.Project;
import com.zzmcc.jms.model.ProjectEquip;
import com.zzmcc.jms.model.ProjectSafeguard;
import com.zzmcc.sys.model.Userinfo;
import util.ChristStringUtil;
import util.ModelUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ProjectSafeguardController extends BaseController {
    private static final Logger log =  Logger.getLogger(ProjectSafeguardController.class);
	public void index() {
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();

		String name = getPara("project.name");
		String code = getPara("project.code");
		String buildType = getPara("project.buildType");
		String homeCounty = getPara("project.homeCounty");
		String modifyDate = getPara("project.modifyDate");

		if(ChristStringUtil.isNotEmpty(name)){
			whee.append(" and a.name like ?");
			param.add("%" + name + "%");
		}
		if(ChristStringUtil.isNotEmpty(buildType)){
			whee.append(" and buildType like ?");
			param.add("%" + buildType + "%");
		}
		if(ChristStringUtil.isNotEmpty(code)){
			whee.append(" and a.code like ?");
			param.add("%" + code + "%");
		}
		if(ChristStringUtil.isNotEmpty(homeCounty)){
			whee.append(" and a.homeCounty = ?");
			param.add(homeCounty);
		}

		if(ChristStringUtil.isNotEmpty(modifyDate)){
			whee.append(" and TO_DAYS(modifyDate) >= TO_DAYS(?)");
			param.add( modifyDate );
		}

		Page<ProjectSafeguard> pager = ProjectSafeguard.dao.paginate(
				getParaToInt("pageNum", 1),
				getParaToInt("numPerPage", 20),
				"select a.homeCounty,a.cmName,a.ctName,a.cuName,\n" +
						"a.towerType,a.name,a.longitude,a.latitude,a.addr,a.buildType,\n" +
						"b.id,b.pid,b.isProject,b.projectDate,b.code,\n" +
						"c.isGuard,c.guardReason,d.createDate,d.modifyDate,d.modifyMemo,d.isPower,d.projectType",
				" from (\n" +
						"\tselect pid,max(a.homeCounty) homeCounty,max(b.isScreen) isScreen,max(a.isConfirm) isConfirm,\n" +
						"\tmax(case when cusName='移动' then a.name end) cmName,\n" +
						"\tmax(case when cusName='电信' then a.name end) ctName,\n" +
						"\tmax(case when cusName='联通' then a.name end) cuName,\n" +
						"\tmax(b.towerType) towerType,\n" +
						"\tmax(c.ctName) name,max(c.buildType) buildType,max(c.longitude) longitude,max(c.latitude) latitude,MAX(c.addr) addr\n" +
						"\tfrom jms_demand a\n" +
						"\tLEFT JOIN jms_demand_screen b\n" +
						"\ton a.did=b.did\n" +
						"\tLEFT JOIN jms_demand_order c\n" +
						"\ton a.did=c.did\n" +
						"\twhere a.isConfirm='是'\n" +
						"\tGROUP BY pid)a\n" +
						"LEFT JOIN jms_project b\n" +
						"on a.pid=b.pid\n" +
						"LEFT JOIN jms_project_build c\n" +
						"on a.pid=c.pid\n" +
						"LEFT JOIN jms_project_safeguard d\n" +
						"on a.pid=d.pid  where 1=1 "+ whee.toString()+" ORDER BY d.modifyDate desc",
				param.toArray());

		setAttr("pager",pager);
		setAttr("name",name);
		setAttr("buildType",buildType);
		setAttr("code",code);
		setAttr("homeCounty",homeCounty);



		setAttr("modifyDate",modifyDate);
		render("../project_safeguard_list.html");
	}

	@Before(AuthorizeInterceptor.class)
	public void add() {
		String id = getPara("id");
		Userinfo loginUser =getSessionAttr("loginUser");
		String modifyUser=loginUser.get("realname");
		int roleid=loginUser.get("roleid");

		String sql="select a.name,a.pid as apid,a.code as acode," +
				"b.*"+
				" from jms_project a left join jms_project_safeguard b on b.pid=a.pid " +
				" where a.id=?";
		ProjectSafeguard m = ProjectSafeguard.dao.findFirst(sql, id);
		setAttr("ProjectSafeguard",m);
		setAttr("modifyUser", modifyUser);
		setAttr("roleid",roleid);
		render("../project_safeguard_input.html");
	}


	public void update() {
		ProjectSafeguard m = getModel(ProjectSafeguard.class, "ProjectSafeguard");
		ModelUtils.fillForUpdate(m);
		m.update();
		renderDWZSuccessJson("修改成功！");
	}


	public void save() {
		try{
			ProjectSafeguard m = getModel(ProjectSafeguard.class, "ProjectSafeguard");
			ModelUtils.fillForUpdate(m);
			m.save();
			renderDWZSuccessJson("添加成功！");
		}catch (Exception e){
			renderDWZSuccessJson("重复提交，失败！");
		}
	}



	public void export(){

		//导出条件
		StringBuffer whee = new StringBuffer();
		List<Object> param = new ArrayList<Object>();

		String name = getPara("project.name");
		String code = getPara("project.code");
		String buildType = getPara("project.buildType");
		String homeCounty = getPara("project.homeCounty");
		String modifyDate = getPara("project.modifyDate");

		if(ChristStringUtil.isNotEmpty(name)){
			whee.append(" and a.name like ?");
			param.add("%" + name + "%");
		}
		if(ChristStringUtil.isNotEmpty(buildType)){
			whee.append(" and buildType like ?");
			param.add("%" + buildType + "%");
		}
		if(ChristStringUtil.isNotEmpty(code)){
			whee.append(" and a.code like ?");
			param.add("%" + code + "%");
		}
		if(ChristStringUtil.isNotEmpty(homeCounty)){
			whee.append(" and a.homeCounty = ?");
			param.add(homeCounty);
		}

		if(ChristStringUtil.isNotEmpty(modifyDate)){
			whee.append(" and TO_DAYS(modifyDate) >= TO_DAYS(?)");
			param.add( modifyDate );
		}

		String sql="SELECT a.homeCounty,a.cmName,a.ctName,a.cuName,\n" +
				"a.towerType,a.name,a.longitude,a.latitude,a.addr,a.buildType,\n" +
				"b.pid,b.isProject,b.projectDate,b.code,\n" +
				"c.isGuard,c.guardReason,c.isComplete,d.*\n" +
				"from (\n" +
				"\tselect pid,max(a.homeCounty) homeCounty,max(b.isScreen) isScreen,max(a.isConfirm) isConfirm,\n" +
				"\tmax(case when cusName='移动' then a.name end) cmName,\n" +
				"\tmax(case when cusName='电信' then a.name end) ctName,\n" +
				"\tmax(case when cusName='联通' then a.name end) cuName,\n" +
				"\tmax(b.towerType) towerType,\n" +
				"\tmax(c.ctName) name,max(c.buildType) buildType,max(c.longitude) longitude,max(c.latitude) latitude,MAX(c.addr) addr\n" +
				"\tfrom jms_demand a\n" +
				"\tLEFT JOIN jms_demand_screen b\n" +
				"\ton a.did=b.did\n" +
				"\tLEFT JOIN jms_demand_order c\n" +
				"\ton a.did=c.did\n" +
				"\twhere a.isConfirm='是'\n" +
				"\tGROUP BY pid)a\n" +
				"LEFT JOIN jms_project b\n" +
				"on a.pid=b.pid\n" +
				"LEFT JOIN jms_project_build c\n" +
				"on a.pid=c.pid\n" +
				"LEFT JOIN jms_project_safeguard d\n" +
				"on a.pid=d.pid where 1=1 "+ whee.toString()+" \n order by d.modifyDate desc";

		List<Record> projectSafeguard = Db.find(sql, param.toArray());

		Map<String, Object> beans = new HashMap<String, Object>();
		beans.put("ps", projectSafeguard);

		String templateFileName = PathKit.getRootClassPath() + "/tpl_project_safeguard.xls";
		String filename = "project_safeguard.xls";
		render(JxlsRender.me(templateFileName).filename(filename).beans(beans));
	}

	@Before(AuthorizeInterceptor.class)
	public void delete() {

		String[] ids = getParaValues("ids");
		for(String id:ids){
			Project m =Project.dao.findById(id);
			Db.update("delete a from jms_project_safeguard a where pid =?", m.get("pid"));
		}
		renderDWZSuccessJson("删除成功!");

	}

}
